﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DC2010;
using DC2010.Objects;
using DC2010.Objects.Data;
using DC2010.Tools;

namespace DC2010_EditorInfo
{
    public partial class CreaturesForm : Form
    {
        public CreatureInfo CreatureInfo { get; set; }
        public string OriginalId { get; set; }

        public CreaturesForm()
        {
            InitializeComponent();

            CreatureInfo = new CreatureInfo();
        }

        private void CreaturesForm_Load(object sender, EventArgs e)
        {
            InitializeLB();
            //InitializeEffectLogicCombo();
        }

        private void InitializeLB()
        {
            imageListBox1.Items.Clear();

            foreach (CreatureInfo ei in Root.I.CreatureSystem.DictInfo.Values)
            {

                ImageListBoxItem ilbi = new ImageListBoxItem(ei.Id, Properties.Resources.Effect);
                imageListBox1.Items.Add(ilbi);
            }
        }

        private void crystalButton1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(OriginalId))
            {
                //we are updating
                GatherInfo(CreatureInfo);
                Root.I.CreatureSystem.SaveInfo();
                InitializeLB();
            }
            else
            {
                //creating new
                GatherInfo(CreatureInfo);
                Root.I.CreatureSystem.AddInfo(CreatureInfo);
                Root.I.CreatureSystem.SaveInfo();
                InitializeLB();
            }
        }

        private void GatherInfo(CreatureInfo ei)
        {
            ei.Id = IdTB.Text;
            ei.ShortName = ShortTB.Text;
            ei.LongName = DescriptionTB.Text;
            ei.DivinePoints = DivinePointsTB.Text.ToInt();
            ei.Effects = keyValueUC1.Data;
            ei.Conditions = keyValueUC2.Data;
            ei.Params = keyValueUC3.Data;

        }

        private void FillWithObject()
        {
            if (CreatureInfo != null)
            {
                OriginalId = CreatureInfo.Id;
                IdTB.Text = CreatureInfo.Id;
                ShortTB.Text = CreatureInfo.ShortName;
                DescriptionTB.Text = CreatureInfo.LongName;
                DivinePointsTB.Text = CreatureInfo.DivinePoints.ToString();
                keyValueUC1.Data = CreatureInfo.Effects;
                keyValueUC2.Data = CreatureInfo.Conditions;
                keyValueUC3.Data = CreatureInfo.Params;

                keyValueUC1.FillList();
                keyValueUC2.FillList();
                keyValueUC3.FillList();

            }
        }

        private void imageListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string s = ((ImageListBoxItem)imageListBox1.SelectedItem).Text;
            CreatureInfo = Root.I.CreatureSystem.GetInfo(s);
            FillWithObject();
        }

        private void crystalButton2_Click(object sender, EventArgs e)
        {
            CreatureInfo = new CreatureInfo();
            OriginalId = "";
        }
    }
}
